import { login, getLeftMenu } from '@/api/user.js'
import { getToken, setToken, removeToken, setTokenTime } from '@/utils/auth.js'
const state = {
  token: getToken(),
  userInfo: {},
  LeftMenuList: [] // 左侧菜单栏
}

const mutations = {
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 获取用户信息
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  // 删除token
  removeToken(state, payload) {
    state.token = payload
    state.userInfo = {}
    removeToken()
  },
  // 获取左侧菜单
  setLeftMenu(state, payload) {
    state.LeftMenuList = payload
  }
}

const actions = {
  // 登录
  async login(context, payload) {
    const res = await login(payload)
    console.log(res)
    setTokenTime()
    context.commit('setToken', res.data.token)
    context.commit('setUserInfo', res.data)
  },
  // 左侧菜单栏
  async getLeftMenu(context) {
    const res = await getLeftMenu()
    context.commit('setLeftMenu', res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

